import os
from PyPDF2 import PdfMerger


def single_file(common_string):
    target_path = "D:\\新版极客时间\\" + common_string  ## pdf目录文件
    pdf_lst = [f for f in os.listdir(target_path) if f.endswith('.pdf')]
    pdf_lst = [os.path.join(target_path, filename) for filename in pdf_lst]

    file_merger = PdfMerger()
    for pdf in pdf_lst:
        file_merger.append(pdf, import_outline=False)  # 合并pdf文件

    file_merger.write(common_string + ".pdf")
    print("finish single file: " + common_string)


def multi_file(folder):
    # 获取目录下的文件夹名称
    pdf_lst = [f for f in os.listdir(folder)]
    for pdf in pdf_lst:
        single_file(pdf)
        print("start single file: " + pdf)


if __name__ == '__main__':
    folder = "D:\新版极客时间"
    multi_file(folder)
